package com.project.erp.service.system.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.project.erp.dao.system.PrivilegeDao;
import com.project.erp.model.system.Privilege;
import com.project.erp.model.system.Role;
import com.project.erp.service.system.PrivilegeService;

@Service
@Transactional
public class PrivilegeServiceImpl implements PrivilegeService{

	@Autowired
	private PrivilegeDao privilegeDao;
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public List<Privilege> queryPrivileges(Privilege privilege){
		List<Privilege> allList =  privilegeDao.queryPrivileges(privilege);
		for (Privilege p : allList) {
			p.setRoles(null);
			p.setParent(null);
		}
		return allList;
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public List<Privilege> getPrivilegeParentByRoot() {
		return privilegeDao.getPrivilegeParentByRoot();
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public Privilege getPrivilegeTreeById(Long privilegeId) {
		return privilegeDao.getPrivilegeTree(privilegeId);
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public List<Privilege> getAllPrivilegeMenuTree(){
		List<Privilege> allList = privilegeDao.getAllPrivilegeMenuTree();
		List<Privilege> list = new ArrayList<Privilege>();
		//获取所有的父节点数据
		for (Privilege privilege : allList) {
			if(null == privilege.getParentId()){
				list.add(privilege);
			}
			privilege.setRoles(null);
		}
		if(null != list && list.size() > 0){
			//去掉根节点的数据信息
			allList.removeAll(list);
			//处理成树形结构
			handerTreeData(list,allList);
		}
		return list;
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public List<Privilege> getAllPrilegeTree(Long roleId){
		//TODO需要根据角色查询权限进行设置check状态
		List<Long> roleIds = new ArrayList<Long>();
		roleIds.add(roleId);
		List<Privilege> rolePrivilege = privilegeDao.getPrivilegeByRoles(roleIds);
		List<Privilege> allList = privilegeDao.getAllPrilegeTree();
		List<Privilege> list = new ArrayList<Privilege>();
		//获取所有的父节点数据
		for (Privilege privilege : allList) {
			if(null == privilege.getParentId()){
				list.add(privilege);
				//设置checked状态
				if(rolePrivilege.contains(privilege)){
					privilege.setChecked(true);
				}
			}
			privilege.setRoles(null);
		}
		if(null != list && list.size() > 0){
			//去掉根节点的数据信息
			allList.removeAll(list);
			//处理成树形结构
			handerRightTreeData(list,allList,rolePrivilege);
		}
		return list;
	}
	
	public void handerTreeData(List<Privilege> parentList,List<Privilege> allList){
		//根据所有父节点数据封装成树形结构
		List<Privilege> removeList = new ArrayList<Privilege>();
		for(Privilege privilege : parentList){
			for(Privilege temp : allList){
				//不能用 == 判断是否相等
				if(privilege.getId().equals(temp.getParentId())){
					privilege.addChild(temp);
					removeList.add(temp);
				}
			}
		}
		if(null != removeList && removeList.size() > 0){
			allList.removeAll(removeList);
		}
		for(Privilege privilege : parentList){
			if(null != privilege.getChildren() && privilege.getChildren().size() > 0){
				handerTreeData(privilege.getChildren(),allList);
			}
		}
	}
	
	public void handerRightTreeData(List<Privilege> parentList,List<Privilege> allList,List<Privilege> rolePrivilege){
		//根据所有父节点数据封装成树形结构
		List<Privilege> removeList = new ArrayList<Privilege>();
		for(Privilege privilege : parentList){
			for(Privilege temp : allList){
				//不能用 == 判断是否相等
				if(privilege.getId().equals(temp.getParentId())){
					privilege.addChild(temp);
					removeList.add(temp);
				}
			}
		}
		if(null != removeList && removeList.size() > 0){
			allList.removeAll(removeList);
		}
		for(Privilege privilege : parentList){
			//设置checked状态
			if(rolePrivilege.contains(privilege)){
				privilege.setChecked(true);
			}
			if(null != privilege.getChildren() && privilege.getChildren().size() > 0){
				handerRightTreeData(privilege.getChildren(),allList,rolePrivilege);
			}
		}
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void savePrivilege(Privilege privilege){
		if(null == privilege.getParent().getId()){
			privilege.setParent(null);
			privilegeDao.save(privilege);
		}else{
			privilegeDao.save(privilege);
			List<Long> parentIds = new ArrayList<Long>();
			Privilege temp = privilege.getParent();
			while(null != temp){
				parentIds.add(temp.getId());
				temp = privilegeDao.get(temp.getId()).getParent();
			}
			//如果是添加新的一级菜单,第一个权限默认是有权限访问的
			List<Privilege> childrens = privilegeDao.getChildrenByParentId(privilege.getParent().getId());
			if(null != childrens && childrens.size() == 1){
				privilegeDao.updateRolePrivilegeById(privilege.getParent().getId(),privilege.getId());
			}
			//删除角色权限菜单关联的父节点数据
			privilegeDao.deleteRolePrivilegeById(parentIds);
		}
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void editPrivilege(Privilege privilege){
		privilegeDao.save(privilege);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public boolean delPrivilege(Long privilegeId){
		
		privilegeDao.deleteRolePrivilegeById(privilegeId);
		privilegeDao.delete(privilegeId);
		return true;
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public Privilege getPrivilegeById(Long id){
		return privilegeDao.get(id);
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public List<Privilege> getPrivilegeByParentId(Long parentId){
		if(null == parentId){
			return null;
		}
		return privilegeDao.getChildrenByParentId(parentId);
	}
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public List<Privilege> getPrivilegeByRoles(List<Role> roles){
		if(null == roles || roles.size() == 0){
			return new ArrayList<Privilege>();
		}
		List<Long> roleIds = new ArrayList<Long>();
		for (Role role : roles) {
			roleIds.add(role.getId());
		}
		List<Privilege> list = privilegeDao.getPrivilegeByRoles(roleIds);
		//需要处理父节点数据信息
		List<Privilege> privileges = handParentPrivilege(list);
		return privileges;
	}
	
	private List<Privilege> handParentPrivilege(List<Privilege> list){
		Set<Privilege> parentPrivileges = new HashSet<Privilege>();
		for (Privilege privilege : list) {
			parentPrivileges.add(privilege);
			Privilege temp = privilege.getParent();
			while(null != temp){
				parentPrivileges.add(temp);
				temp = privilegeDao.get(temp.getId()).getParent();
			}
		}
		List<Privilege> privileges = new ArrayList<Privilege>(parentPrivileges);
		//根据ID排序
		Collections.sort(privileges);
		return privileges;
	}
}
